package algorithm.leetcode.array;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: util
 * @description: 找到数组中两数相加之和等于给定数
 * 输出这个数字和这个数字的下标
 * @author: zhouliangliang
 * @create: 2021-03-30 16:59
 **/
public class AddAimNum {

    private int[]  solve(int []num, int target) {
        //值，下标
        Map<Integer, Integer> map = new HashMap<>();
        for(int i=0; i<num.length; i++) {
            int minus = target - num[i];
            if(map.get(minus) == null) {
                map.put(num[i], i);
            } else {
                return new int []{i, map.get(minus)};
            }
        }
        return new int []{-1,-1};
    }

    public static void main(String []args) {
        Arrays.stream(new AddAimNum().solve(new int []{2,7,11,15}, 9)).forEach(item ->{
            System.out.println(item);
        });
    }

}
